package com.xxxx.server.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.EmployeeImport;
import com.xxxx.server.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Scope("prototype")
public class EmployeeExcelListener extends AnalysisEventListener<EmployeeImport> {


    List<EmployeeImport> employees = new ArrayList<>();

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void invoke(EmployeeImport employee, AnalysisContext analysisContext) {

       employees.add(employee);
       if(employees.size()%5 == 0) {
            employeeService.readExcel(employees);
            employeeService.save(employees);
            employees.clear();
       }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}
